package com.yunhai.ucenter.config.security;

import com.yunhai.common.dto.UserInfoDto;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        //1、从请求头中获取 token
//        String token = response.getHeader("token");
//        if( !StringUtils.hasText(token)){
//            //如果 token 不存在，则直接放行，由后面的拦截器进行拦截。
//            filterChain.doFilter(request, response);
//            return;
//        }

        //暂时直接放行
        UserInfoDto userInfoDto = new UserInfoDto();
        UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userInfoDto, null, null);
        //这个就是告诉 spring security 用户已经登录。
        SecurityContextHolder.getContext().setAuthentication(authentication);

        //放行
        filterChain.doFilter(request, response);
    }
}
